<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-row>
        <el-form-item label="论文编号" prop="productNo">
          <el-input v-model="queryParams.productNo" placeholder="请输入论文编号" clearable size="small" style="width: 232px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="论文名称" prop="objName">
          <el-input v-model="queryParams.objName" placeholder="请输入论文名称" clearable size="small" style="width: 232px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

      </el-row>
      <el-row>
        <el-form-item label="客户名称" prop="proName">
          <el-input v-model="queryParams.proName" placeholder="请输入客户名称" clearable size="small" style="width: 232px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

          <el-form-item label="收藏时间" prop="createTimeArray">
            <el-date-picker v-model="queryParams.createTimeArray" type="daterange" align="right" clearable style="width: 232px"
              value-format="yyyy-MM-dd" unlink-panels range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
        </el-form-item>
        <el-form-item style="margin-left: 10px;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row class="row-mbinfo">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['order:cart:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['order:cart:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['order:cart:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <!--
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['order:cart:export']">导出</el-button>
          -->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="divTable">
      <el-table border v-loading="loading" :data="cartList" >

        <el-table-column label="论文编号" align="left" prop="productNo" width="150">
        </el-table-column>
        <el-table-column label="论文标题" align="left" prop="objName" width="350">
        </el-table-column>
        <el-table-column label="客户名称" align="center" prop="customerName" />
        <el-table-column label="论文类型" align="center" prop="lunwenType" />
        <el-table-column label="字数" align="center" prop="numbers" />
        <el-table-column label="收藏时间" align="center" prop="collectTime" width="200">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.collectTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

  </div>
</template>

<script>
  import {
    listCart,
    getCart,
    delCart,
    addCart,
    updateCart
  } from "@/api/order/cart";

 import ImageUpload from '@/components/ImageUpload';

  export default {
    name: "Cart",
    components: {
      ImageUpload
    },
    data() {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 购物车明细表格数据
        cartList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 状态字典
        cartStatusOptions: [],
        // 活动类型字典
        activityTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          supplierUid: null,
          proName: null,
          skuDesc: null,
          cartStatus: null,
          activityType: null,
          createTime: null,
          createTimeArray: [],
        },
        columns: [{
            label: 'ID',
            dataIndex: 'cartUid',
          },
          {
            label: '平台ID',
            dataIndex: 'platformUid',
          },
          {
            label: '供应商ID',
            dataIndex: 'supplierUid',
          },
          {
            label: '商品ID',
            dataIndex: 'proUid',
          },
          {
            label: '商品名称',
            dataIndex: 'proName',
          },
          {
            label: '商品封面图',
            dataIndex: 'proImgUrl',
          },
          {
            label: '商品 SKU ID',
            dataIndex: 'skuUid',
          },
          {
            label: 'SKU图片',
            dataIndex: 'skuImgUrl',
          },
          {
            label: 'SKU 描述',
            dataIndex: 'skuDesc',
          },
          {
            label: '价格',
            dataIndex: 'price',
          },
          {
            label: '展示价格',
            dataIndex: 'showPrice',
          },
          {
            label: '数量',
            dataIndex: 'cartNumber',
          },
          {
            label: '合计金额',
            dataIndex: 'cartAmount',
          },
          {
            label: '积分',
            dataIndex: 'score',
          },
          {
            label: '状态',
            dataIndex: 'cartStatus',
          },
          {
            label: '活动ID',
            dataIndex: 'activityUid',
          },
          {
            label: '活动类型',
            dataIndex: 'activityType',
          },
          {
            label: '活动产品ID',
            dataIndex: 'activityProUid',
          },
          {
            label: '创建人',
            dataIndex: 'createUid',
          },
          {
            label: '创建时间',
            dataIndex: 'createTime',
          },
          {
            label: '更新人',
            dataIndex: 'updateUid',
          },
          {
            label: '更新时间',
            dataIndex: 'updateTime',
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          proUid: [{
            required: true,
            message: "商品ID不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      // 默认时间
      let date1 = new Date();
      let date2 = new Date(date1);
      date2.setDate(date1.getDate() - 7);
      let time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
      let time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
      this.queryParams.createTimeArray.push(time2);
      this.queryParams.createTimeArray.push(time1);
      this.getList();
      this.getDicts("order_cart_status").then(response => {
        this.cartStatusOptions = response.data;
      });
      this.getDicts("activity_activity_type").then(response => {
        this.activityTypeOptions = response.data;
      });
    },
    methods: {
      /** 查询购物车明细列表 */
      getList() {
        this.loading = true;
        listCart(this.queryParams).then(response => {
          this.cartList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      cartStatusFormat(row, column) {
        return this.selectDictLabel(this.cartStatusOptions, row.cartStatus);
      },
      // 活动类型字典翻译
      activityTypeFormat(row, column) {
        return this.selectDictLabel(this.activityTypeOptions, row.activityType);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          cartUid: null,
          platformUid: null,
          supplierUid: null,
          proUid: null,
          proName: null,
          proImgUrl: null,
          skuUid: null,
          skuImgUrl: null,
          skuDesc: null,
          price: null,
          showPrice: null,
          cartNumber: null,
          cartAmount: null,
          score: null,
          cartStatus: null,
          activityUid: null,
          activityType: null,
          activityProUid: null,
          createUid: null,
          createTime: null,
          updateUid: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.createTimeArray = null;
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.cartUid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加购物车明细";
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true;
        this.viewTitle = "查看购物车明细";
        this.showData = [];
        this.columns.forEach(item => {
          this.showData.push({
            label: item.label,
            value: row[item.dataIndex]
          })
        })
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const cartUid = row.cartUid || this.ids
        getCart(cartUid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改购物车明细";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.cartUid != null) {
              updateCart(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCart(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const cartUids = row.cartUid || this.ids;
        this.$confirm('是否确认删除购物车明细编号为"' + cartUids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCart(cartUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('order/cart/export', {
          ...this.queryParams
        }, `order_cart.xlsx`)
      }
    }
  };
</script>
<style lang="scss" scoped>
  .item_sku_pro {
    display: flex;
    align-items: center;
    .item_pro_sku_info {
      height: 50px;
      flex: 1;
      overflow: hidden;
      margin-left: 10px;
      padding: 5px 0;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }
  }

  .treeOne {
    display: inline-block;
    margin-right: 200px;
  }

  .treeTwo {
    display: inline-block;
  }
</style>
